﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.BLL;
using OgilvyOne.Model;

using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using System.Collections.Generic;
using ECCIT;
using ECCIT.Web;

public partial class admin_UKNOW_News : AdminBasePage
{
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_PAGE_NO = "pageNum";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            ViewState[VIEWSTATE_COLUMN_ID] = Request.Params[VIEWSTATE_COLUMN_ID];
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params[""]);
            ViewState[VIEWSTATE_PAGE_NO] = "1";//如果不是回发,即首次加载页面,则令,ViewState[VIEWSTATE_PAGE_NO] = "1",页面显示的是GridView1的第一页;

            hdnPopupUrl.Value = GetPopupUrl();
            Grid_viewBild();
        }
        else
        {
            if (string.IsNullOrEmpty(ViewState[VIEWSTATE_PAGE_NO].ToString())) return;
            string sPageNum = Request.Params[VIEWSTATE_PAGE_NO];
            if (ViewState[VIEWSTATE_PAGE_NO].ToString() != sPageNum)
            {
                ViewState[VIEWSTATE_PAGE_NO] = sPageNum;
                Grid_viewBild();
            }
        }
        
    }


    private void Grid_viewBild()
    {
        int columnid = int.Parse(Request.Params["columnid"]);
        ColumnModel cm = new ColumnBll().GetColumn(columnid);
        IList<NewsModel> newsls = new NewsBLL(LangID).selectNews();
        GridView1.DataSource = newsls;
        //int number=0;
        //if (int.TryParse(ViewState[VIEWSTATE_PAGE_NO].ToString(), out number))
        //{
        //    string[] str = (ViewState[VIEWSTATE_PAGE_NO].ToString()).Split(',');
        //    if (str.Length > 0)
        //    {
        //        number = int.Parse(str[0]);
        //    }
        //    else
        //    {
        //        number = int.Parse(ViewState[VIEWSTATE_PAGE_NO].ToString()) - 1;
        //    }
        //}

        GridView1.PageIndex = int.Parse(ViewState[VIEWSTATE_PAGE_NO].ToString()) - 1;
        GridView1.DataBind();
        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, newsls.Count);
    }

    //按新闻标题搜索活动记录
    protected void Grid_viewBildPart(object sender, EventArgs e)
    {
        string newName = newTittle.Text;
        int? state = Convert.ToInt32(newState.SelectedValue);
        int columnid = int.Parse(Request.Params["columnid"]);
        ColumnModel cm = new ColumnBll().GetColumn(columnid);
        IList<NewsModel> searchNewsByTittle = new NewsBLL(LangID).selNewsByTittle(newName, state);
        GridView1.DataSource = searchNewsByTittle;
        GridView1.PageIndex = int.Parse(ViewState[VIEWSTATE_PAGE_NO].ToString()) - 1;
        GridView1.DataBind();
        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, searchNewsByTittle.Count);
        newTittle.Text = "";
        newState.SelectedValue = "2";
    }

    protected void Del_click(object sender, EventArgs e)
    {
        int nid = int.Parse(((LinkButton)sender).CommandArgument);
        ResultInfo res = new NewsBLL(LangID).delNews(nid);
        if (res.Result)
        {
            Response.Write("<script>alert('" + res.Message + "')</script>");
            //Response.Redirect(GetRedirectUrl());
        }
        else
        {
            Response.Write(res.Message);
        }
        //删除该新闻的列表图片以及数据库中的记录
        Grid_viewBild();
    }
    protected void Pubone_click(object sender, EventArgs e)
    {
        //修改单个新闻的发布状态
        int nid = int.Parse(((LinkButton)sender).CommandArgument);
        new NewsBLL(LangID).modPubOne(nid);
        Grid_viewBild();
    }
    protected void PhoneImg_click(object sender, EventArgs e)
    {
        //Response.Redirect("news_phone_image_list.aspx?" + ReturnQueryString(true) + "&EventsID=" + ((LinkButton)sender).CommandArgument, true);

        Response.Redirect(string.Format("news_phone_image_list.aspx?{0}={1}&{2}={3}&{4}={5}&{6}={7}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID],
            "langID", this.LangID, "newID", ((LinkButton)sender).CommandArgument));
    }//mff
    private string GetPopupUrl()
    {
        string url = string.Format("new_add.aspx?{0}={1}&{2}={3}&{4}={5}", 
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID], 
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID], 
            "langID", this.LangID);
        return url;
    }

    private string GetRedirectUrl()
    {
        string url = string.Format("news.aspx?{0}={1}&{2}={3}&{4}={5}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID],
            "langID", this.LangID);
        return url;
    }
}